USE ASLB 
GO
IF (OBJECTPROPERTY ( OBJECT_ID('EndSessions'), 'IsProcedure' ) IS NOT NULL)
BEGIN
   DROP PROC EndSessions
END
GO

SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER OFF
GO


CREATE PROC [dbo].[EndSessions] 
       @DatabaseName Varchar(50) = NULL
      ,@ServerName   Varchar(50) = NULL
       
AS
/***********************************************************************************
** Name      : EndSessions
**
** File Name : EndSessions.sql
**
** Desc      : 
**
** Called By : 
**
** Calls     : 
**
** Returns   :
** Author    : jocaplan
**
** Date      : 04/02/2009
**
** Parameters: 
                

** Usage     :
               EXEC EndSessions
                  @DatabaseName = 'MSSalesBI'
                  ,@ServerName = 'COLTS'

************************************************************************************
** Change History
************************************************************************************
** Date        Author     Description
** ----------  ---------  ----------------------------------------------------------
**********************************************************************************/

SET NOCOUNT ON


DELETE [Session]
WHERE SessionID in(
   SELECT S.SessionID
   FROM 
      [Session] S
      JOIN [Server] SE
         ON S.ServerID = SE.ServerID
      JOIN [Database] DB
         ON S.DatabaseID = DB.DatabaseID
   WHERE
      SE.Name like ISNULL(@ServerName, '%')
      AND DB.Name like ISNULL(@DatabaseName, '%')
)


SELECT * FROM [Session]